<template>
  <div class="login">
    <section class="form_container">
      <div class="manage_tip">
        <span class="title">资金后台管理系统</span>
        <el-form-renderer
          label-width="100px"
          :content="content"
          ref="registerForm"
          class="loginForm"
        >
          <el-button
            @click="submitForm(registerForm)"
            type="primary"
            class="submit_btn"
            >登 录</el-button
          >
          <div class="tiparea">
            <p>
              还没有账号？现在<router-link to="/register">注册</router-link>
            </p>
          </div>
        </el-form-renderer>
      </div>
    </section>
  </div>
</template>
<script setup>
import { ref } from "vue";
import axios from "../utils/http";
const registerForm = ref("");
import { ElMessage } from "element-plus";
import { useRouter } from "vue-router";

import { useUserStore } from "../store/user";

import { jwtDecode } from "jwt-decode";
const userStore = useUserStore();
const router = useRouter();

const content = [
  {
    type: "input",
    id: "email",
    label: "邮箱",
    rules: [
      {
        type: "email",
        required: true,
        message: "邮箱格式不正确",
        trigger: "blur",
      },
    ],
  },
  {
    type: "input",
    id: "password",
    el: {
      type: "password",
    },
    label: "密码",
    rules: [
      { required: true, message: "密码不能为空", trigger: "blur" },
      { min: 6, max: 30, message: "长度在 6 到 30 个字符", trigger: "blur" },
    ],
  },
];
const submitForm = async (formEl) => {
  if (!formEl) return;
  let valid = await formEl.methods.validate();

  if (valid) {
    try {
      let params = registerForm.value.getFormValue();
      let { code, data } = await axios.post("/api/users/login", params);
      if (code != 200) {
        // 注册成功
        ElMessage({
          message: data.message || "未知错误",
          type: "error",
        });
        return;
      }
      localStorage.setItem("eleToken", data.token);
      // 解析token
      const decode = jwtDecode(data.token);
      console.log(decode);

      // 存储数据;
      userStore.SET_IS_AUTNENTIATED(!isEmpty(decode));
      userStore.SET_USER(decode);

      // 页面跳转
      router.push("/index");
    } catch (error) {
      console.log(error);
    }
  }
};
// 验证空值 符合为真
const isEmpty = (value) => {
  return (
    value === undefined ||
    value === null ||
    (typeof value === "object" && Object.keys(value).length === 0) ||
    (typeof value === "string" && value.trim().length === 0)
  );
};
</script>

<style scoped lang="scss">
.login {
  position: relative;
  width: 100%;
  height: 100%;
  background: url(../assets/bg.jpg) no-repeat center center;
  background-size: 100% 100%;
}
.form_container {
  width: 370px;
  height: 210px;
  position: absolute;
  top: 20%;
  left: 34%;
  padding: 25px;
  border-radius: 5px;
  text-align: center;
}
.form_container .manage_tip .title {
  font-family: "Microsoft YaHei";
  font-weight: bold;
  font-size: 26px;
  color: #fff;
}
.loginForm {
  margin-top: 20px;
  background-color: #fff;
  padding: 20px 40px 20px 20px;
  border-radius: 5px;
  box-shadow: 0px 5px 10px #cccc;
}

.submit_btn {
  width: 100%;
}
.tiparea {
  text-align: right;
  font-size: 12px;
  color: #333;
  margin-top: 15px;
}
.tiparea p a {
  color: #409eff;
}
</style>
